//
//  GoodsCollectionCell.m
//  Yanwei
//
//  Created by 刘文 on 2017/12/8.
//  Copyright © 2017年 DCloud. All rights reserved.
//

#import "GoodsCollectionCell.h"
#import "NSString+Util.h"

static NSString *GoodsCollectionCellClass = @"GoodsCollectionCellIdentifier";

@interface GoodsCollectionCell ()

/** 背景视图 */
@property (nonatomic, strong) UIView *contentBackgroundView;
/** 商品图片 */
@property (nonatomic, strong) UIImageView *storeImageView;
/** 商品标题Label */
@property (nonatomic, strong) UILabel *storeTitleLabel;
/** 商品描述Label */
@property (nonatomic, strong) UILabel *storeDescribeLabel;
/** 商品当前价格 */
@property (nonatomic, strong) UILabel *storeNowPriceLabel;
/** 商品以往价格 */
@property (nonatomic, strong) UILabel *storeOldPriceLabel;
/** 销量 */
@property (nonatomic, strong) UILabel *salesVolumeLabel;
/** 选择框 */
@property (strong, nonatomic) UILabel * selectLabel;

@end

@implementation GoodsCollectionCell

+ (instancetype)initTableView:(UITableView *)tableView {
    GoodsCollectionCell * cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([GoodsCollectionCell class])];
    if (!cell) {
        cell = [[GoodsCollectionCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NSStringFromClass([GoodsCollectionCell class])];
    }
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
    }
    return self;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

#pragma mark - setting
- (void)setCollectionViewCellModel:(GoodsModel *)collectionViewCellModel {
    /**
    [self.storeImageView sd_setImageWithURL:[NSURL URLWithString:collectionViewCellModel.mainImagePath] placeholderImage:NewStoreDefault options:0];
    self.storeTitleLabel.text = collectionViewCellModel.title;
    
    if (collectionViewCellModel.goodsClassify == GoodsClassifyTypeExchangeGoods) {
        self.storeNowPriceLabel.text = @"580 积分";
        self.storeOldPriceLabel.text = [NSString stringWithFormat:@"市场参考价：79.00元"];
    } else {
        if (collectionViewCellModel.sellingPrice.hasValue) {
            self.storeNowPriceLabel.text = [NSString stringWithFormat:@"¥%@", collectionViewCellModel.sellingPrice];
        } else {
            self.storeNowPriceLabel.text = nil;
        }
        
        self.storeOldPriceLabel.text = @"";
        if (collectionViewCellModel.regularPrice.hasValue && ([collectionViewCellModel.regularPrice floatValue] > [collectionViewCellModel.sellingPrice floatValue])) {
            self.storeOldPriceLabel.hidden = NO;
            NSString *regularPriceStr = [NSString stringWithFormat:@"¥%@", collectionViewCellModel.regularPrice];

            NSString *newPriceStr = [NSString stringWithFormat:@"市场均价%@", regularPriceStr];
            NSMutableAttributedString *newPrice = [[NSMutableAttributedString alloc] initWithString:newPriceStr];
            [newPrice addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlinePatternSolid | NSUnderlineStyleSingle) range:[newPriceStr rangeOfString:regularPriceStr]];
            self.storeOldPriceLabel.attributedText = newPrice;
        } else {
            self.storeOldPriceLabel.hidden = YES;
        }
    }
    
    if (collectionViewCellModel.checked) {
        self.selectLabel.textColor = Color175030;
        self.selectLabel.text = SELECT_D;
    } else {
        self.selectLabel.textColor = HEXCOLOR(0xcacaca);
        self.selectLabel.text = SELECT_N;
    }
     */
}

- (void)setEditAction:(BOOL)editAction {
    _editAction = editAction;
    
    self.selectLabel.hidden = !editAction;
    if (editAction) {
        [self.contentBackgroundView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView).with.offset(30);
        }];
    } else {
        [self.contentBackgroundView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView);
        }];
    }
}


#pragma mark - 懒加载
- (UIView *)contentBackgroundView {
    if (!_contentBackgroundView) {
        _contentBackgroundView = [[UIView alloc] init];
        [self.contentView addSubview:_contentBackgroundView];
        [_contentBackgroundView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.contentView);
            make.left.equalTo(self.contentView);
            make.bottom.equalTo(self.contentView);
            make.width.equalTo(self.contentView);
        }];
    }
    return _contentBackgroundView;
}
/** 商品图片 */
- (UIImageView *)storeImageView {
    if (!_storeImageView) {
        _storeImageView = [UIImageView new];
        [self.contentBackgroundView addSubview:_storeImageView];
        [_storeImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.contentBackgroundView).with.offset(10);
            make.bottom.equalTo(self.contentBackgroundView).with.offset(-10);
            make.left.equalTo(self.contentBackgroundView).with.offset(16);
            make.width.equalTo(self.storeImageView.mas_height);
        }];
    }
    return _storeImageView;
}
/** 商品标题 */
- (UILabel *)storeTitleLabel {
    if (!_storeTitleLabel) {
        _storeTitleLabel = [UILabel new];
        _storeTitleLabel.font = APPFONT(16);
        _storeTitleLabel.textColor = Color53;
        _storeTitleLabel.numberOfLines = 2;
        [self.contentBackgroundView addSubview:_storeTitleLabel];
        [_storeTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.storeImageView).with.offset(5);
            make.left.equalTo(self.storeImageView.mas_right).with.offset(14);
            make.right.equalTo(self.contentBackgroundView).with.offset(-18);
        }];
    }
    return _storeTitleLabel;
}
/** 当前价格 */
- (UILabel *)storeNowPriceLabel {
    if (!_storeNowPriceLabel) {
        _storeNowPriceLabel = [UILabel new];
        _storeNowPriceLabel.font = [UIFont fontWithName:@"PingFangSC Medium" size:14];
        _storeNowPriceLabel.textColor = Color175030;
        [self.contentBackgroundView addSubview:_storeNowPriceLabel];
        [_storeNowPriceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.storeTitleLabel);
            make.bottom.equalTo(self.storeImageView.mas_bottom);
        }];
    }
    return _storeNowPriceLabel;
}
/** 以往价格 */
- (UILabel *)storeOldPriceLabel {
    if (!_storeOldPriceLabel) {
        _storeOldPriceLabel = [UILabel new];
        _storeOldPriceLabel.font = APPFONT(10);
        _storeOldPriceLabel.textColor = RGBACOLOR(159, 159, 159, 1);
        [self.contentBackgroundView addSubview:_storeOldPriceLabel];
        [_storeOldPriceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.storeNowPriceLabel.mas_right);
            make.bottom.equalTo(self.storeNowPriceLabel);
        }];
    }
    return _storeOldPriceLabel;
}
- (UILabel *)selectLabel {
    if (!_selectLabel) {
        _selectLabel = [UILabel new];
        _selectLabel.font = ICONFONT(15);
        _selectLabel.textColor = HEXCOLOR(0xcacaca);
        _selectLabel.userInteractionEnabled = NO;
        _selectLabel.hidden = YES;
        [self.contentView addSubview:_selectLabel];
        [_selectLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(7);
            make.centerY.equalTo(self.contentView);
            make.width.and.height.mas_equalTo(17);
        }];
        
        __weakConfigure
        [_selectLabel tapUpWithBlock:^(UIView * v) {
            if (__weakSelf.clickedCheckBlock) {
                /**
                __weakSelf.collectionViewCellModel.checked = !__weakSelf.collectionViewCellModel.checked;
                 **/
                __weakSelf.clickedCheckBlock(__weakSelf.indexPath);
            }
        }];
    }
    return _selectLabel;
}
- (UILabel *)salesVolumeLabel {
    if (!_salesVolumeLabel) {
        _salesVolumeLabel = [UILabel new];
        _salesVolumeLabel.textColor = Color140;
        _salesVolumeLabel.font = APPFONT(10);
        [self.contentView addSubview:_salesVolumeLabel];
        [_salesVolumeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.storeTitleLabel);
            make.top.equalTo(self.storeNowPriceLabel.mas_bottom).with.offset(9);
        }];
    }
    return _salesVolumeLabel;
}


#pragma mark - public methods
+ (NSString *)cellClass {
    return GoodsCollectionCellClass;
}

@end
